package cn.iocoder.yudao.module.ocr.controller.admin.result;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ocr.controller.admin.result.vo.*;
import cn.iocoder.yudao.module.ocr.dal.dataobject.result.ResultDO;
import cn.iocoder.yudao.module.ocr.service.result.ResultService;

@Tag(name = "管理后台 - ocr结果")
@RestController
@RequestMapping("/ocr/result")
@Validated
public class ResultController {

    @Resource
    private ResultService resultService;

    @PostMapping("/create")
    @Operation(summary = "创建ocr结果")
    @PreAuthorize("@ss.hasPermission('ocr:result:create')")
    public CommonResult<Long> createResult(@Valid @RequestBody ResultSaveReqVO createReqVO) {
        return success(resultService.createResult(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ocr结果")
    @PreAuthorize("@ss.hasPermission('ocr:result:update')")
    public CommonResult<Boolean> updateResult(@Valid @RequestBody ResultSaveReqVO updateReqVO) {
        resultService.updateResult(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ocr结果")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ocr:result:delete')")
    public CommonResult<Boolean> deleteResult(@RequestParam("id") Long id) {
        resultService.deleteResult(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ocr结果")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ocr:result:query')")
    public CommonResult<ResultRespVO> getResult(@RequestParam("id") Long id) {
        ResultDO result = resultService.getResult(id);
        return success(BeanUtils.toBean(result, ResultRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ocr结果分页")
    @PreAuthorize("@ss.hasPermission('ocr:result:query')")
    public CommonResult<PageResult<ResultRespVO>> getResultPage(@Valid ResultPageReqVO pageReqVO) {
        PageResult<ResultDO> pageResult = resultService.getResultPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ResultRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ocr结果 Excel")
    @PreAuthorize("@ss.hasPermission('ocr:result:export')")
    @OperateLog(type = EXPORT)
    public void exportResultExcel(@Valid ResultPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResultDO> list = resultService.getResultPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ocr结果.xls", "数据", ResultRespVO.class,
                        BeanUtils.toBean(list, ResultRespVO.class));
    }

}